*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*%%%
*%%% ANALYSIS
*%%%
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

*%%% FIGURE 1: Effect of infrastructure development on early-life mortality (per 1,000)
*%%% TABLE 1:  Dynamic effect of infrastructure development on early-life mortality
*%%% TABLE 2:  Static effect of infrastructure development on early-life mortality 
*%%% TABLE 3:  Dynamic effect of infrastructure development on early-life mortality, by cause of death
*%%% FIGURE 2: Effect of infrastructure development on early-life mortality, by cause of death
*%%% TABLE 4:  Heterogeneous effects by intensity


*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*%%%
*%%% FIGURE 1: Effect of infrastructure development on early-life mortality (per 1,000)
*%%%
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

use "$data/analysis_district_clean_all", clear

qui {

foreach y of varlist vs_imr1y  { 
	
// OLS
reghdfe `y' F*event L*event, a(i t) cluster(i) //
testparm F*event
estimates store ols

// CS
cap drop gvar
gen gvar = cond(Ei==., 0, Ei) // group variable as required for the csdid command
csdid `y', ivar(i) time(t) gvar(gvar) notyet agg(event) long2
estat event, estore(cs) // this produces and stores the estimates at the same time

event_plot ols cs, stub_lag(L#event Tp#) stub_lead(F#event Tm#) plottype(scatter) ciplottype(rcap)  ///
together perturb(0(0.2)0.4) trimlead(6) trimlag(7) noautolegend ///
graph_opt(xline(0, lcolor(gs8) lpattern(dash)) yline(0, lcolor(gs8)) graphregion(color(white) margin(zero)) bgcolor(white) ylabel(, angle(horizontal)) ///
xtitle("Years since start of implementation") ytitle("Average causal effect") title("") $xaxis_start $yaxis_imr /// 
legend(order(1 "OLS" 3 "Callaway-Sant'Anna (2021)") rows(1) pos(12)) name(gimr1, replace)) ///
		lag_opt1(color(black) msymbol(S)) lag_ci_opt1(color(black%60 black%60) lwidth(thick)) ///
		lag_opt2(color(black) msymbol(Th)) lag_ci_opt2(color(black%60 black%60) lwidth(medthick) lp(dash)) ///
		alpha(0.1)
graph export "$graphs/event1_`y'.pdf", replace

est clear

}


foreach y of varlist vs_u5mr {

reghdfe `y' F*event L*event, a(i t) cluster(i) //
testparm F*event

estimates store ols

// CS
cap drop gvar
gen gvar = cond(Ei==., 0, Ei) // group variable as required for the csdid command
csdid `y', ivar(i) time(t) gvar(gvar) notyet agg(event) long2
estat event, estore(cs) // this produces and stores the estimates at the same time

event_plot ols cs, stub_lag(L#event Tp#) stub_lead(F#event Tm#) plottype(scatter) ciplottype(rcap)  ///
together perturb(0(0.2)0.4) trimlead(6) trimlag(7) noautolegend ///
graph_opt(xline(0, lcolor(gs8) lpattern(dash)) yline(0, lcolor(gs8)) graphregion(color(white) margin(zero)) bgcolor(white) ylabel(, angle(horizontal)) ///
xtitle("Years since start of implementation") ytitle("Average causal effect") title("") $xaxis_start $yaxis_u5mr /// 
legend(off) name(gu5mr1, replace)) ///
		lag_opt1(color(black) msymbol(S)) lag_ci_opt1(color(black%60 black%60) lwidth(thick)) ///
		lag_opt2(color(black) msymbol(Th)) lag_ci_opt2(color(black%60 black%60) lwidth(medthick) lp(dash)) ///
		alpha(0.1)
graph export "$graphs/event1_`y'.pdf", replace

est clear

}
}



*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*%%%
*%%% TABLE 1: Dynamic effect of infrastructure development on early-life mortality
*%%%
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

use "$data/analysis_district_clean_all", clear

drop *event*

//Get leads and lags for binned periods
gen L1event = K>=0 & K<=3
gen L2event = K>=4 & K<=7
gen L3event = K>=8 & K<=10

gen F1event = K==-1
gen F2event = K<=-2 & K>=-10

drop F1event
gen F1event=0

lab var L1event "0 to 3"
lab var L2event "4 to 7"
lab var L3event "8 to 10"
lab var F2event "--10 to --2"
		
		
*MORTALITY ALL CAUSES 
*----------------------				
	
qui{
foreach y of varlist vs_imr1y  { 

reghdfe `y' F*event L*event, a(i t) cluster(i) 
estimates store m1 
stats_table_a `y'
stats_table_pvalue `y'

}



foreach y of varlist vs_u5mr {

reghdfe `y' F*event L*event, a(i t) cluster(i) 
estimates store m2
stats_table_a `y'
stats_table_pvalue `y'
		
}		

}


lab var F2event "Pre (--10 to --2)"
lab var L1event "Post 1 (0 to 3)"
lab var L2event "Post 2 (4 to 7)"
lab var L3event "Post 3 (8 to 10)"

esttab m1 m2 using "$tables/event1_agg_MR.tex", replace f nonumb nomtitles noeqlines nodepvars nogaps plain parentheses ///
cells(b(fmt(2)) se(fmt(2) par) p(fmt(2) par([ ]))) label collabels(none) keep(F2event L1event L2event L3event) ///
stats(test_diff1 test_diff2 test_diff3 test_diff4 test_diff5 Cmean N Nc, fmt(%9.2f %9.2f %9.2f %9.2f %9.2f %9.2f %9.0f %9.0f) ///
labels("[1ex] \\[-0.7em]  Pre -- Post 1 (\textit{p}-value)" "Pre -- Post 2 (\textit{p}-value)" "Pre -- Post 3 (\textit{p}-value)" "Post 1 -- Post 2 (\textit{p}-value)" "Post 2 -- Post 3 (\textit{p}-value)"  "[1ex] \\[-0.7em] Mean (pre-start)"  "\hline District-years" "Districts"))	


				
				
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*%%%
*%%% TABLE 2: Static effect of infrastructure development on early-life mortality 
*%%%
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%				
		
		
use "$data/analysis_district_clean_all", clear

label var D "Implementation"


qui{
	
*IMR
*------------	
local count = 1
foreach y of varlist vs_imr1y vs_imr1y_san vs_imr1y_accid vs_imr1y_resp vs_imr1y_other {
	
	if `count' == 1 | `count' == 2 | `count' == 3 | `count' == 4 | `count' == 5 {

reghdfe `y' D, a(i t) cluster(i)
estimates store m`count' 
stats_table_a `y'
	
}

		local count = `count' + 1
		
}

esttab m* ///
using "$tables/DD_IMR.tex", replace f nonumb nomtitles noeqlines nodepvars nogaps plain parentheses ///
cells(b(fmt(2)) se(fmt(2) par) p(fmt(2) par([ ]))) label collabels(none) keep(D) ///
stats(Cmean, fmt(%9.2f) ///
labels("[1ex] \\[-0.7em] Mean (pre-start)"))	

est clear

*U5MR
*------------	
local count = 1
foreach y of varlist vs_u5mr vs_u5mr_san vs_u5mr_accid vs_u5mr_resp vs_u5mr_other  {
	
	if `count' == 1 | `count' == 2 | `count' == 3 | `count' == 4 | `count' == 5 {

reghdfe `y' D, a(i t) cluster(i)
estimates store m`count'
stats_table_a `y' 
	
}

		local count = `count' + 1
		
}

esttab m* ///
using "$tables/DD_U5MR.tex", replace f nonumb nomtitles noeqlines nodepvars nogaps plain parentheses ///
cells(b(fmt(2)) se(fmt(2) par) p(fmt(2) par([ ]))) label collabels(none) keep(D) ///
stats(Cmean N Nc, fmt(%9.2f %9.0f %9.0f) ///
labels("[1ex] \\[-0.7em] Mean (pre-start)"  "\hline District-years" "Districts"))	

est clear

}
		

		
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*%%%
*%%% TABLE 3: Dynamic effect of infrastructure development on early-life mortality, by cause of death
*%%%
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

use "$data/analysis_district_clean_all", clear

drop *event*

//Get leads and lags for binned periods
gen L1event = K>=0 & K<=3
gen L2event = K>=4 & K<=7
gen L3event = K>=8 & K<=10

gen F1event = K==-1
gen F2event = K<=-2 & K>=-10

drop F1event
gen F1event=0

lab var L1event "0 to 3"
lab var L2event "4 to 7"
lab var L3event "8 to 10"
lab var F2event "--10 to --2"
		
		
*MORTALITY BY COD
*--------------------		
		
qui{
	
local count = 1
foreach y of varlist vs_imr1y_san vs_imr1y_accid vs_imr1y_resp vs_imr1y_other {
	
	if `count' == 1 | `count' == 2 | `count' == 3 | `count' == 4 {

reghdfe `y' F*event L*event, a(i t) cluster(i)
estimates store ols`count' 
stats_table_a `y'
stats_table_pvalue `y'

}

		local count = `count' + 1
		
}


}



qui{
	
local count = 5
foreach y of varlist vs_u5mr_san vs_u5mr_accid vs_u5mr_resp vs_u5mr_other {
	
	if `count' == 5 | `count' == 6 | `count' == 7 | `count' == 8 {

reghdfe `y' F*event L*event, a(i t) cluster(i)
estimates store ols`count' 
stats_table_a `y'
stats_table_pvalue `y'

}

		local count = `count' + 1
		
}

}

esttab ols1 ols2 ols3 ols4 ols5 ols6 ols7 ols8 using "$tables/event1_agg_MRCOD.tex", replace f nonumb nomtitles noeqlines nodepvars nogaps plain parentheses ///
cells(b(fmt(2)) se(fmt(2) par) p(fmt(2) par([ ]))) label collabels(none) keep(F2event L1event L2event L3event) ///
stats(test_diff1 test_diff2 test_diff3 test_diff4 test_diff5 Cmean N Nc, fmt(%9.2f %9.2f %9.2f %9.2f %9.2f %9.2f %9.0f %9.0f) ///
labels("[1ex] \\[-0.7em]  Pre -- Post 1 (\textit{p}-value)" "Pre -- Post 2 (\textit{p}-value)" "Pre -- Post 3 (\textit{p}-value)" "Post 1 -- Post 2 (\textit{p}-value)" "Post 2 -- Post 3 (\textit{p}-value)"  "[1ex] \\[-0.7em] Mean (pre-start)"  "\hline District-years" "Districts"))	

eststo clear
		
		
		
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*%%%
*%%% FIGURE 2: Effect of infrastructure development on early-life mortality, by cause of death
*%%%
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

use "$data/analysis_district_clean_all", clear

qui{
	
local count = 1
foreach y of varlist vs_imr1y_san vs_imr1y_accid vs_imr1y_resp vs_imr1y_other {
	
	if `count' == 1 | `count' == 2 | `count' == 3 | `count' == 4 | `count' == 5 {

reghdfe `y' F*event L*event, a(i t) cluster(i)
estimates store ols`count' 

}

		local count = `count' + 1
		
}

event_plot ols1 ols2 ols3 ols4, stub_lag(L#event) stub_lead(F#event) plottype(scatter) ciplottype(rcap) ///
graph_opt(xline(0, lcolor(gs8) lpattern(dash)) yline(0, lcolor(gs8)) graphregion(color(white)) bgcolor(white) ylabel(, angle(horizontal)) ///
xtitle("Years since start of implementation") ytitle("Average causal effect") title("") $xaxis_start $yaxis_imr2 legend(order(1 "Waterborne" 3 "Accidents" 5 "Respiratory" 7 "Other") rows(1))) ///
together perturb(-0.6(0.2)0.6)  noautolegend ///
 trimlead(6) trimlag(7) ///
	lag_opt1(msymbol(O) color(black)) lag_ci_opt1(color(black) lwidth(medium))  ///
	lag_opt2(msymbol(T) color(gs8)) lag_ci_opt2(color(gs8) lwidth(medium))  ///
	lag_opt3(msymbol(Oh) color(gs4)) lag_ci_opt3(color(gs4) lwidth(thin))   ///
	lag_opt4(msymbol(X) color(gs10)) lag_ci_opt4(color(gs10) lwidth(thin)) alpha(0.1)
	
graph export "$graphs/event1_IMR_cod.pdf", replace

est drop ols1 ols2 ols3 ols4


}


qui{
	
local count = 1
foreach y of varlist vs_u5mr_san vs_u5mr_accid vs_u5mr_resp vs_u5mr_other {
	
	if `count' == 1 | `count' == 2 | `count' == 3 | `count' == 4 | `count' == 5 {

reghdfe `y' F*event L*event, a(i t) cluster(i)
estimates store ols`count' 

}

		local count = `count' + 1
		
}

event_plot ols1 ols2 ols3 ols4, stub_lag(L#event) stub_lead(F#event) plottype(scatter) ciplottype(rcap) ///
graph_opt(xline(0, lcolor(gs8) lpattern(dash)) yline(0, lcolor(gs8)) graphregion(color(white)) bgcolor(white) ylabel(, angle(horizontal)) ///
xtitle("Years since start of implementation") ytitle("Average causal effect") title("") $xaxis_start $yaxis_u5mr2 legend(order(1 "Waterborne" 3 "Accidents" 5 "Respiratory" 7 "Other") rows(1))) ///
together perturb(-0.6(0.2)0.6)  noautolegend ///
 trimlead(6) trimlag(7) ///
	lag_opt1(msymbol(O) color(black)) lag_ci_opt1(color(black) lwidth(medium))  ///
	lag_opt2(msymbol(T) color(gs8)) lag_ci_opt2(color(gs8) lwidth(medium))  ///
	lag_opt3(msymbol(Oh) color(gs4)) lag_ci_opt3(color(gs4) lwidth(thin))   ///
	lag_opt4(msymbol(X) color(gs10)) lag_ci_opt4(color(gs10) lwidth(thin)) alpha(0.1)
	
graph export "$graphs/event1_U5MR_cod.pdf", replace

est drop ols1 ols2 ols3 ols4


}


*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*%%%
*%%% TABLE 4: Heterogeneous effects by intensity
*%%%
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


use "$data/analysis_district_clean_all", clear


foreach het of varlist pscore { //geographic suitability
	cap drop `het'm 
	cap drop *event_`het'
	
	sum `het' if year ==2005, det
	gen `het'm=`het'>=`r(p50)' if `het'!=.
	gen D_`het'=D*`het'm

lab var D "Implementation"
}

lab var D_pscore "Implementation $\times$ High intensity"



*REDUCED FORM: Heterogeneity by implementation
*-------------------------------------------

global het "D_pscore"

*IMR
*------------	
local count = 1
foreach y of varlist vs_imr1y vs_imr1y_san vs_imr1y_accid vs_imr1y_resp vs_imr1y_other {
	
	if `count' == 1 | `count' == 2 | `count' == 3 | `count' == 4 | `count' == 5 {

reghdfe `y' D $het, a(i t) cluster(i)
estimates store m`count' 
stats_table_a `y'
	
}

		local count = `count' + 1
		
}

esttab m* ///
using "$tables/RF_IMR.tex", replace f nonumb nomtitles noeqlines nodepvars nogaps plain parentheses ///
cells(b(fmt(2)) se(fmt(2) par) p(fmt(2) par([ ]))) label collabels(none) keep(D D_*) ///
stats(Cmean, fmt(%9.2f) ///
labels("[1ex] \\[-0.7em] Mean (pre-start)"))	

est clear

*U5MR
*------------	
local count = 1
foreach y of varlist vs_u5mr vs_u5mr_san vs_u5mr_accid vs_u5mr_resp vs_u5mr_other  {
	
	if `count' == 1 | `count' == 2 | `count' == 3 | `count' == 4 | `count' == 5 {

reghdfe `y' D $het, a(i t) cluster(i)
estimates store m`count'
stats_table_a `y' 
	
}

		local count = `count' + 1
		
}

esttab m* ///
using "$tables/RF_U5MR.tex", replace f nonumb nomtitles noeqlines nodepvars nogaps plain parentheses ///
cells(b(fmt(2)) se(fmt(2) par) p(fmt(2) par([ ]))) label collabels(none) keep(D D_*) ///
stats(Cmean N Nc, fmt(%9.2f %9.0f %9.0f) ///
labels("[1ex] \\[-0.7em] Mean (pre-start)"  "\hline District-years" "Districts"))	

est clear


